package com.base.vistter.iframe.service;

import com.base.vistter.iframe.entity.InverterDevicesPowerDay;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.mapper.InverterDevicesPowerDayMapper;
import com.base.vistter.iframe.model.DevicesDayPowerRequest;
import com.base.vistter.iframe.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
@Slf4j
public class InverterDevicesPowerDayService extends BaseService<InverterDevicesPowerDayMapper, InverterDevicesPowerDay> implements Serializable {

    public static final String DEFAULT_TABLE_NAME = "inverter_devices_power_day";

    public static final String DEVICES_POWER_PREFIX = "inverter_devices_power_day_";

    public static final DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

    @Transactional(readOnly = true)
    public Map<String, Float> getTodayStationElectric(Integer inverterStationId) throws IframeException {
        List<InverterDevicesPowerDay> list = this.getTodayElectric(inverterStationId);
        Float p = (float) list.stream().mapToDouble(InverterDevicesPowerDay::getP).sum(); //获取功率
        Float dp = (float) list.stream().mapToDouble(InverterDevicesPowerDay::getDP).sum(); //获取发电量
        return Map.of("p", p, "dp", dp);
    }

    @Transactional(readOnly = true)
    public Map<String, Float> getTodayElectric() throws IframeException {
        return this.getTodayStationElectric(null);
    }

    /**
     * 获取电站今日发电量
     */
    @Transactional(readOnly = true)
    public List<InverterDevicesPowerDay> getTodayElectric(Integer stationId) {
        return this.getBaseMapper().getDayElectric(stationId, DateUtils.getTodayStartTime(), DateUtils.getTodayEndTime());
    }

    /**
     * 日查询设备的统计
     */
    public List<InverterDevicesPowerDay> statisticsDevices(DevicesDayPowerRequest devicesDayPowerReqest) {
        //如果是今天直接获取数据
        if (DateUtils.isToday(devicesDayPowerReqest.getTime())) {
            String column = Arrays.stream(devicesDayPowerReqest.getColumns()).map(item -> "`" + item + "`").collect(Collectors.joining(","));
            return this.getBaseMapper().statisticsDevices(column, devicesDayPowerReqest.getDevicesId(), DateUtils.getDateStartTime(devicesDayPowerReqest.getTime()), DateUtils.getDateEndTime(devicesDayPowerReqest.getTime()));
        } else {
            String tableName = DEVICES_POWER_PREFIX + dateFormat.format(devicesDayPowerReqest.getTime());
            if (this.getBaseMapper().checkTableExists(tableName) > 0) {
                String column = Arrays.stream(devicesDayPowerReqest.getColumns()).map(item -> "`" + item + "`").collect(Collectors.joining(","));
                return this.getBaseMapper().statisticsDevicesHistroy(column, devicesDayPowerReqest.getDevicesId(), tableName, DateUtils.getDateStartTime(devicesDayPowerReqest.getTime()), DateUtils.getDateEndTime(devicesDayPowerReqest.getTime()));
            }
        }
        return null;
    }


    @Transactional(readOnly = true)
    public List<InverterDevicesPowerDay> statisticsDay(Date time, Integer stationId) {
        if (DateUtils.isToday(time)) {
            return this.baseMapper.statisticsStation(stationId, DEFAULT_TABLE_NAME, DateUtils.getDateStartTime(time), DateUtils.getDateEndTime(time));
        }else{
            String tableName = DEVICES_POWER_PREFIX + dateFormat.format(time);
            if(this.baseMapper.checkTableExists(tableName) > 0){
                return this.baseMapper.statisticsStation(stationId, tableName, DateUtils.getDateStartTime(time), DateUtils.getDateEndTime(time));
            }
            return null;
        }
    }

}
